package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.service.WxPayService;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;


import com.wechat.pay.java.service.partnerpayments.app.model.PrepayResponse;


import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * wx pay service impl
 *
 * @author QA
 * @date 2025/07/03 15:32
 */
@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {

	@Autowired
	private PaymentInfoService paymentInfoService;

	/**
	 * 微信下单
	 *
	 * @param paymentType 支付类型
	 * @param orderNo     订单号
	 * @return {@link Map<String, String>}
	 */
	@Autowired
	RSAAutoCertificateConfig rsaAutoCertificateConfig;
	@Autowired
	private WxPayV3Config wxPayV3Config;

	@Override
	public Map<String, String> createJsapi(String paymentType, String orderNo) {
		Long userId = AuthContextHolder.getUserId();
		PaymentInfo paymentInfo = paymentInfoService.savePaymentInfo(orderNo, paymentType, userId);
//		对接微信支付
		JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig).build();
		// request.setXxx(val)设置所需参数，具体参数可见Request定义
		PrepayRequest request = new PrepayRequest();
		Amount amount = new Amount();
		amount.setTotal(1);
		request.setAmount(amount);
		request.setAppid(wxPayV3Config.getAppid());
		request.setMchid(wxPayV3Config.getMerchantId());
		request.setDescription(paymentInfo.getContent());
		request.setNotifyUrl("https://notify_url");
		request.setOutTradeNo(paymentInfo.getOutTradeNo());
		Payer payer = new Payer();
		payer.setOpenid("ogXUE7i1sGTQ5sOqHl2PEqx7pEtk");
		request.setPayer(payer);
		// 调用下单方法，得到应答
		PrepayWithRequestPaymentResponse prepayWithRequestPaymentResponse = service.prepayWithRequestPayment(request);
		//获取对应信息

		HashMap<String, String> hashMap = new HashMap<String, String>();
		if (prepayWithRequestPaymentResponse != null) {
			hashMap.put("package", prepayWithRequestPaymentResponse.getPackageVal());
			hashMap.put("timeStamp", prepayWithRequestPaymentResponse.getTimeStamp());
			hashMap.put("nonceStr", prepayWithRequestPaymentResponse.getNonceStr());
			hashMap.put("signType", prepayWithRequestPaymentResponse.getSignType());
			hashMap.put("paySign", prepayWithRequestPaymentResponse.getPaySign());

		// 使用微信扫描 code_url 对应的二维码，即可体验Native支付


	}
		return hashMap;
	}
}
